﻿/***************************************************************
*       
* add by OceanHo 2015/8/28 10:41:44
*
****************************************************************/

using oceanho.webapi.factory.httpCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace oceanho.webapi.factory.httpUtility
{
    /// <summary>
    /// 定义一个工具类。包含基础的 HttpRequest辅助方法
    /// </summary>
    public sealed class RequestUtility
    {
        /// <summary>
        /// 获取请求客户端的唯一标识符
        /// </summary>
        /// <param name="request">HttpRequest对象</param>
        /// <returns></returns>
        internal static string GetReqID(HttpRequest request)
        {
            return MD5Utility.Encrypt(
                string.Concat(RequestUtility.GetSchemeHostAndPort(
                request), RequestUtility.GetIP(request)), MD5Mode.Default);
        }

        /// <summary>
        /// 获取请求客户端的IP地址（有可能获取到的是代理IP地址）
        /// </summary>
        /// <param name="request">HttpRequest对象</param>
        /// <returns></returns>
        internal static string GetIP(HttpRequest request)
        {
            return request.UserHostAddress;
        }

        /// <summary>
        /// 获取请求客户端的方案名称(http/https)+://+主机名+:+端口+/
        /// </summary>
        /// <param name="request">HttpRequest对象</param>
        /// <returns></returns>
        public static string GetSchemeHostAndPort(HttpRequest request)
        {
            return request.Url.Scheme + "://" + request.Url.Host + ":" + request.Url.Port + "/";
        }
    }
}
